package com.example.edu.exceptionhandler;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import com.example.utils.Result;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 全局异常处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result globalException(Exception e) {
        e.printStackTrace();
        return Result.error().message("启用了全局异常处理");
    }

    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result specularException(ArithmeticException e) {
        e.printStackTrace();
        return Result.error().message("启用了特定异常处理");
    }

    @ExceptionHandler(HeartEduException.class)
    @ResponseBody
    public Result customizeException(HeartEduException e) {
        e.printStackTrace();
        return Result.error().code(e.getCode()).message(e.getMsg());
    }
}
